﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.IO;

namespace KingOfCSharp.iHunter.Plugins.BayesianFilter
{
    public class BayesianFilter : IFilter
    {
        private double strength = 0.0;
        private BayesianFilterImpl _filter = null;
        private Corpus _bad, _good;
        private System.Data.DataSet dataSet;

        /// <summary>
        /// This version only support strength to be its parameter
        /// Need to support more
        /// Such as the trained file that needs to be loaded
        /// </summary>
        /// <param name="settings"></param>
        public BayesianFilter(string settings, System.Data.DataSet dataSet)
        {
            // Filter settings:
            //
            // <Filter>
            //   <Strength>
            //       <!-- A threshold, double number from 0.0 to 1.0
            //            0.0 means all pass, 1.0 means all filtered -->
            //       #.####
            //   </Strength>
            //   <Model>
            //       <!-- The existed model file -->
            //       Model file path
            //   </Model>
            // </Filter>
            //
            this.dataSet = dataSet;
            if (dataSet.Tables.Count == 0)
            {
                // A new table needs to be created by the resource
            }
            XmlDocument xml = new XmlDocument();
            xml.LoadXml(settings);
            strength = double.Parse(xml.DocumentElement["Strength"].InnerText);
            if (xml.DocumentElement["Model"].InnerText != "")
            {
                _filter.FromFile(xml.DocumentElement["Model"].InnerText);
            }
        }

  
        #region IDisposable Members

        public void Dispose()
        {
            // Currently do nothing;
        }

        #endregion

        #region IFilter Members

        /// <summary>
        /// This version only support content based spam filter
        /// And only English is supported
        /// Chinese is much harder, waiting for the next version...
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool FilterMessage(IMessage message)
        {
            if (_filter == null)
            {
                MessageBox.Show("Load model first!");
                return false;
            }

            double spamProb = _filter.Filtrate(message.Content);
            return (spamProb > strength) ? true : false;
        }

        /// <summary>
        /// This version is a pile of shit, needs better interface
        /// And needs to save the model file for future use
        /// Coordinate with jbx later...
        /// </summary>
        /// <param name="message"></param>
        /// <param name="result"></param>
        public void Train(IMessage message, bool result)
        {
            if (result)
            {
                if (_bad == null)
                {
                    _bad = new Corpus(new StringReader(message.Content));
                }
                else
                {
                    _bad.LoadFromReader(new StringReader(message.Content));
                }
            }
            else
            {
                if (_good == null)
                {
                    _good = new Corpus(new StringReader(message.Content));
                }
                else
                {
                    _good.LoadFromReader(new StringReader(message.Content));
                }
            }
            if (_filter == null)
            {
                _filter = new BayesianFilterImpl();
            }
            else
            {
                _filter.Load(_good, _bad);
            }
        }

        #endregion
    }
}
